import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-21
 * Time: 14:52
 */
class Solution2 {
    public List<String> summaryRanges(int[] nums) {
        int n = nums.length;
        List<String> list = new ArrayList<>();
        int i = 0 ;
        for(; i < n ;i++){
            int start = nums[i];
            while(i + 1 < n && nums[i+1] - nums[i] == 1){
                i++;
            }
            if(i + 1 == n - 1 && nums[i+1] - nums[i] == 1){
                i = n-1;
            }
            int end = nums[i];
            StringBuilder builder = new StringBuilder();
            if(start != end){
                builder.append(start);
                builder.append("->");
                builder.append(end);
            }else{
                builder.append(start);
            }
            list.add(builder.toString());
        }
        if(i == n-1){
            int last = nums[n-1];
            StringBuilder builder = new StringBuilder();
            builder.append(last);
            list.add(builder.toString());
            return list;
        }
        return list;
    }
}
public class Test2 {
}
